
package com.mengmeizi.util;



import com.mengmeizi.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

public class DialogUtil {

    /** 退出提示框 */
    public static CustomDialog showQuitDialog(Context context, Handler handler) {
        return DialogUtil.showAlertDialog(context, handler, null, null,
                context.getResources().getString(R.string.quitInfo),
                context.getResources().getString(R.string.quitMessage), false, "", "", Const.DIALOG_QUIT_BUTTON,
                Const.DIALOG_CANCEL_BUTTON);
    }

    /**
     * 不带CheckBox、不带回传消息的文本弹出框
     * 
     * @param context
     * @param handler
     * @param title
     * @param content
     * @param leftButtonWhat
     * @param rightButtonWhat
     * @return
     */
    public static CustomDialog showAlertDialog(Context context, Handler handler, String title, String content,
            int leftButtonWhat, int rightButtonWhat) {
        return showAlertDialog(context, handler, null, null, title, content, false, "", "", leftButtonWhat,
                rightButtonWhat);
    }

    /**
     * 弹出框
     * 
     * @param context
     * @param handler
     * @param leftButtonMessage 左边按钮的回传消息
     * @param rightButtonMessage 右边按钮的回传消息
     * @param title 弹出框的标题
     * @param content 弹出框显示的内容
     * @param isShowCheckBox 是否显示CheckBox
     * @param leftButtonText 左边按钮文字
     * @param rightButtonText 右边按钮文字
     * @param leftButtonWhat 左边按钮事件（<0：坐标按钮不显示; >0：显示，并可处理相应响应事件）
     * @param rightButtonWhat 右边按钮事件（<0：不显示;>0：显示，并可处理相应响应事件）
     * @return
     */
    public static CustomDialog showAlertDialog(Context context, final Handler handler, final Message leftButtonMessage,
            final Message rightButtonMessage, String title, String content, boolean isShowCheckBox,
            String leftButtonText, String rightButtonText, final int leftButtonWhat, final int rightButtonWhat) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setContent(content);
        if (title != null && !"".equals(title)) {
            dialog.setTitle(title);
        }
        if (!isShowCheckBox) {
            dialog.hideCheckBox();
        }
        if (leftButtonText != null && !"".equals(leftButtonText)) {
            dialog.setLeftButtonText(leftButtonText);
        }
        if (rightButtonText != null && !"".equals(rightButtonText)) {
            dialog.setRightButtonText(rightButtonText);
        }
        if (handler == null && leftButtonWhat <= 0 && rightButtonWhat <= 0) {
            dialog.hideButton();
        } else {
            if (leftButtonWhat > 0) {
                if (rightButtonWhat <= 0) {
                    dialog.leftButton().setBackgroundResource(R.xml.btn_dialog_cancel_selector);
                }
                dialog.setLeftButtonOnClick(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (handler != null) {
                            if (leftButtonMessage == null) {
                                handler.sendEmptyMessage(leftButtonWhat);
                            } else {
                                leftButtonMessage.what = leftButtonWhat;
                                handler.sendMessage(leftButtonMessage);
                            }
                        } else {
                            dialog.dismiss();
                        }
                    }
                });
            } else {
                dialog.hideLeftButton();
            }
            if (rightButtonWhat > 0) {
                if (leftButtonWhat <= 0) {
                    dialog.rightButton().setBackgroundResource(R.xml.btn_dialog_cancel_selector);
                    dialog.rightButton().setText("确定");
                }
                dialog.setRightButtonOnClick(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (handler != null) {
                            if (rightButtonMessage == null) {
                                handler.sendEmptyMessage(rightButtonWhat);
                            } else {
                                rightButtonMessage.what = rightButtonWhat;
                                handler.sendMessage(rightButtonMessage);
                            }
                        } else {
                            dialog.dismiss();
                        }
                    }
                });
            } else {
                dialog.hideRightButton();
            }
        }

        try {
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dialog;
    }

    public static void dismissDialog(CustomDialog dialog) {
        if (dialog != null) {
            try {
                dialog.dismiss();
                dialog = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
